<?php
require_once 'init_app.php';

set_time_limit(0); 

$tags = array();
$tmp_tags = ORM::factory('tag')
            ->find_all()
            ->as_array();
            
foreach ($tmp_tags as $tag) {
    $tags[$tag->id] = $tag;    
}

$products = ORM::factory('product')
                ->where('category_id', '!=', 0)
                ->offset(10000)
                ->limit(200)
                ->find_all();
                
foreach ($products as $product) {
    $ids = DB::select()
                ->from('product_tags')
                ->where('product_id', '=', $product->id)
                ->execute()
                ->as_array();
    $names = array();
    foreach ($ids as $id) {
        if ($tags[$id['tag_id']]->single_form) {
            $names[] = $tags[$id['tag_id']]->single_form;        
        }  
    }   
    
    if (count($names)) {
        $product->names = implode('||', $names);
        $product->save();
        
        //file_put_contents('names.txt', implode('||', $names)."\r\n", FILE_APPEND);   
    }
}